package com.itchen.hospital.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.AnnouncementMapper;
import com.itchen.hospital.mapper.AnnouncementMapper;
import com.itchen.hospital.pojo.Admin;
import com.itchen.hospital.pojo.Announcement;
import com.itchen.hospital.pojo.Params;
import com.itchen.hospital.pojo.User;
import com.itchen.hospital.service.AnnouncementService;
import com.itchen.hospital.service.AnnouncementService;
import com.itchen.hospital.utils.JwtTokenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AnnouncementServiceImpl implements AnnouncementService {

    private final AnnouncementMapper announcementMapper;

    /**
     * 查询所有医生
     *
     * @return
     */
    @Override
    public List<Announcement> getAll() {
        return announcementMapper.getAll();
    }

    //条件查询
    @Override
    public PageInfo<Announcement> search(Params params) {
        //处理分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Announcement> list = announcementMapper.findBySearch(params);
        return PageInfo.of(list);
    }

    //添加
    @Override
    public void add(Announcement announcement) {
        //从请求头中获取用户名
        Admin currentUser = JwtTokenUtils.getCurrentUser();
        if (currentUser != null) {
            announcement.setCreator(currentUser.getUsername());
            announcementMapper.add(announcement);
        }
    }

    //修改
    @Override
    public void update(Announcement announcement) {
        announcementMapper.update(announcement);
    }

    //根据id查询
    @Override
    public Announcement findById(String id) {
        Announcement announcement = announcementMapper.findById(id);
        return announcement;
    }

    //删除
    @Override
    public void delete(String id) {
        announcementMapper.delete(id);
    }

    //批量删除
    @Override
    public void batchDelete(List<Announcement> announcements) {
        announcements.forEach(announcement -> {
            announcementMapper.delete(String.valueOf(announcement.getId()));
        });
    }
}
